package com.dayang.controllor;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.dayang.model.Userif;
import com.dayang.service.UserService;

@Controller
@RequestMapping("/login")
public class LoginContorllor {
	Logger log =LogManager.getLogger(LoginContorllor.class);
	
	@Resource
	private UserService userService;
	
/*	@RequestMapping(value = "login/loginin.do", method = RequestMethod.POST)  
	public String formTag(HttpSession session, HttpServletRequest request) {  
	    String url = request.getHeader("Referer");//获取上个页面的url  
	    String username = request.getParameter("username");
	    String password = request.getParameter("password");
	    if("01".equals(username) && "1".equals(password)){
	    	  System.out.println(username);
	  	    session.setAttribute("redirectUrl", url);//把url放到session  
	  	    session.setAttribute("username", username);//把url放到session  
	  	    return "index"; 	
	    }else{
	    	  System.out.println(username);
	  	    return "login"; 
	    	
	    }
	   
	}  */
	  
	@RequestMapping(value = "/loginin.do", method = RequestMethod.POST)  
	public ModelAndView  formTag(HttpSession session, HttpServletRequest request) {  
	    String url = request.getHeader("Referer");//获取上个页面的url  
	    String username = request.getParameter("username");
	    String password = request.getParameter("password");
	    log.info("获取的用户名是："+username);
	   
	    //List<Userif> list2 = userService.findAll();
	    List<Userif> list = userService.findByUsername(username);
	    try {
	    	 Userif  u = new Userif();
	 	    u.setId("3333312");
	 	    u.setUsername("100");
	 	    u.setUserpassword("100");
	 	    u.setRole(2);
	 	    userService.mergeItem(u);
		} catch (Exception e) {
			e.printStackTrace();
			 log.info("保存用户名是："+e.getMessage());
		}
	    if(list!=null && list.size()>0){
	    	Userif user = list.get(0);
	    	if(user.getUserpassword().equals(password)){
	    		 session.setAttribute("redirectUrl", url);//把url放到session  
	 	  	    session.setAttribute("username", username);//把url放到session  
	 	  	  return new ModelAndView(new RedirectView("../index.jsp"));  
	    	}else{
	    		
	    		return new ModelAndView(new RedirectView("../login.jsp")); 
	    	}
	    	
	    }else{
	    	
	    	return new ModelAndView(new RedirectView("../login.jsp")); 
	    }
	    /*if("01".equals(username) && "1".equals(password)){
	    	
	    	  System.out.println(username);
	  	    session.setAttribute("redirectUrl", url);//把url放到session  
	  	    session.setAttribute("username", username);//把url放到session  
	  	  return new ModelAndView(new RedirectView("../index.jsp"));  
	    }else{
	    	  System.out.println(username);
	    	  return new ModelAndView(new RedirectView("../login.jsp"));  
	    	
	    }*/
	   
	} 
	

}
